package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.PromptManager;
import com.app.utils.TransformController;
import com.app.xutils.MyCallBack;
import com.app.xutils.Xutils;
import com.liaoinstan.springview.container.AliFooter;
import com.liaoinstan.springview.container.AliHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.adapter.VenueAdapter;
import phone.qyg.com.qyg.model.Venues;

/**
 * 场馆预约
 */
@ContentView(R.layout.activity_venue_list)
public class VenueListActivity extends BaseActivity {
    @ViewInject(R.id.back)
    TextView back;
    @ViewInject(R.id.springview)
    SpringView springview;
    @ViewInject(R.id.recycler)
    RecyclerView recycler;
    VenueAdapter adapter;
    List<Venues> list = new ArrayList<>();
    int pageNo = 1;
    int totalpage=1;
    @Override
    public void initSucessView() {
        springview.setHeader(new AliHeader(QXApplication.getContext(),false));
        springview.setFooter(new AliFooter(QXApplication.getContext(),false));
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        adapter = new VenueAdapter(this,list);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter);

        getData();
    }

    private void getData() {
        HashMap<String,String> fMapKeys = new HashMap<String,String>();
        if(CommonUtil.isNetWork()){
            fMapKeys.put(AppHttpKey.PAGE_NO,pageNo+"");
            Xutils.Get(ApplicationConfig.NET_DSN_PATH + ApplicationConfig.VENUE_LIST,
                    fMapKeys,
                    new MyCallBack<String>(){
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            List<Venues> venues = JSONTool.jsonDefaluTranClazzs(result,JSONTool.Enum.VENUE_LIST, Venues.class);
                            totalpage = JSONTool.requestJSONfindNameCount(result,JSONTool.Enum.DATA,JSONTool.Enum.TOTALPAGE);
                            Message message = new Message();
                            message.obj = venues;
                            message.what = 0;
                            mHandler.sendMessage(message);
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            super.onError(ex, isOnCallback);
                            PromptManager.showToast(R.string.network_error);
                        }
                    });
        }else{
            PromptManager.showToast(R.string.network_error);
        }
    }

    @Override
    public void initViewHandler() {
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Object obj = adapter.getDatas().get(position);
                TransformController.transformControllerModel(QXApplication.getContext(), VenuesDetailActivity.class,obj);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        springview.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
              pageNo=1;
              getData();
            }

            @Override
            public void onLoadmore() {
                if(pageNo>=totalpage){
                    PromptManager.showToast(R.string.more_no_datas);
                    springview.onFinishFreshAndLoad();
                    return;
                }
                pageNo++;
                getData();
            }
        });
    }
    @Event(value = R.id.back)
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
        }
    }
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            List<Venues> venues = (List<Venues>) msg.obj;
            switch (msg.what){
                case 0:
                    if(pageNo==1){
                        if(venues==null||venues.size()==0){
                            PromptManager.showToast(R.string.empty_data);
                            return;
                        }
                        list.clear();
                    }else{
                        if(venues==null||venues.size()==0){
                            PromptManager.showToast(R.string.more_no_datas);
                            return;
                        }
                    }
                    list.addAll(venues);
                    adapter.notifyDataSetChanged();
                    springview.onFinishFreshAndLoad();
                    break;
            }
        }
    };
}
